In [1]:
%matplotlib inline
Problem: Write a program that enters a number and prints the numbers from 1 to n through 3 (step
In [2]:
n = int(input())
for i in range(1,n + 1, 3):
print(i)
Problem: Write a program that sets a positive integer n and prints the numbers from n to 1 in reverse order (from the largest to the smallest).
In [5]:
n = int(input())
for i in range(n, 0, -1):
print(i)
Problem: Write a program that reads from the console an integer n and prints the numbers from 1 to 2n.
In [7]:
n = int(input())
for i in range(0, n + 1, 1):
print(pow(2,i))
Problem: Write a program that enters n and prints the even degrees at 2 ≤ 2n: 20, 22, 24, 28, ..., 2n.
In [9]:
n = int(input())
for i in range(0, n + 1, 1):
if i % 2 == 0:
print(pow(2,i))
Problem: Write a program that enters a number n and prints all numbers ≤ n of the row: 1, 3, 7, 15, 31, .... Each subsequent number is calculated as the previous number * 2 + 1.
In [10]:
n = int(input())
result = 1
while result <= n:
print(result)
result = result * 2 + 1
Problem: Write a program that introduces a positive integer n in the range [1 ... 100]. When entering a number outside the specified range, an error message is printed and the user is prompted to enter a new number.
In [12]:
n = int(input('Еnter a number in the range [1...100]: '))
while 1 > n or n > 100:
print("Invalid number!")
n = int(input('Еnter a number in the range [1...100]: '))
print("The number is: "+ str(n))
Problem: Write a program that introduces two integer positive integers a and b and calculates and prints their largest common divisor (NOD).
In [14]:
a = int(input())
b = int(input())
while b != 0:
save = a % b
a = b
b = save
print(a)
Problem: Write a program that enters an integer n (1 ≤ n ≤ 12) and calculates and prints n! = 1 2 ... * n (n factorial).
In [15]:
n = int(input())
result = 1
for i in range(1, n + 1):
result *= i
print(result)
Problem: Write a program that enters an integer num and prints the sum of its numbers.
In [16]:
n = input()
sum = 0
for i in n:
sum += int(i)
print(sum)
Problem: Write a program that enters an integer n and checks whether it is a simple number (whether it is divided by itself and per unit). Print "Prime" or "Not prime".
In [17]:
import math
n = int(input())
is_prime = True
if n < 2:
print('Not Prime')
else:
end = int(math.sqrt(n))
for i in range(2, end + 1):
if n / i == n//i:
is_prime = False
if is_prime:
print('Prime')
else:
print('Not Prime')
Problem: Write a program that introduces an even number. If the user has entered the wrong number, a new one must be entered.
In [18]:
n = float(input('Enter even number: '))
while n % 2 != 0:
print('Invalid number!')
n = int(input('Enter even number: '))
n = print('Even number entered: ' + str(n))
Problem: Write a program that enters an integer and calculates the Fibonacci nth number. The zero figure of Fibonacci is 1, the first one is also 1, and the next is the sum of the previous two.
In [19]:
n = int(input())
a = 1
b = 2
for i in range(1, n):
new_b = a + b
a = b
b = new_b
print(a)